extends Node

const SPAWN_RADIUS=330


@export var enemy_scenes = []
@export var boss_scenes = []
@export var spawn_radius = 1000
@export var min_distance_from_player = 500

# 添加敌人生成信号
signal enemy_spawned(enemy)

var time_to_next_spawn = 0
var spawn_timer_base = 2.0  # 基础生成间隔时间
var difficulty_modifier = 1.0  # 难度修正，随时间增加
var player = null
var player_camera = null
var wave = 1

func _ready():
	player = $"../Player"
	# 获取玩家相机
	if player and player.has_node("Camera2D"):
		player_camera = player.get_node("Camera2D")
		
	# 在这里可以加载敌人预制件
	enemy_scenes.append(preload("res://Scene/Enemy/rock.tscn"))

func _process(delta):
	if not $"../../GameManager".var_game_over:
		time_to_next_spawn -= delta
		
		if time_to_next_spawn <= 0:
			spawn_enemy()
			# 随着游戏进程加快生成速度
			spawn_timer_base = max(0.5, spawn_timer_base - 0.01)
			time_to_next_spawn = spawn_timer_base / difficulty_modifier
		
		# 难度随时间提升
		difficulty_modifier = 1.0 + ($"../../GameManager".game_time / 60.0) * 0.2
		
		# 每5分钟增加一波敌人并增加难度
		if int($"../../GameManager".game_time / 300) + 1 > wave:
			wave += 1
			spawn_wave()

func spawn_enemy():
	if enemy_scenes.size() == 0 or player == null:
		return
		
	var enemy_scene = enemy_scenes[randi() % enemy_scenes.size()]
	var enemy = enemy_scene.instantiate()
	
	# 获取一个在摄像机视野外的生成位置
	var spawn_pos = get_spawn_position_outside_camera()
	
	enemy.global_position = spawn_pos
	add_child(enemy)
	
	# 对敌人进行配置
	if enemy.has_method("initialize"):
		enemy.initialize(difficulty_modifier)
	
	# 发出敌人生成信号
	enemy_spawned.emit(enemy)

# 获取摄像机视野外的生成位置
func get_spawn_position_outside_camera():
	# 默认生成位置
	var random_direction = Vector2.RIGHT.rotated(randf_range(0,TAU))
	var spawn_pos = player.global_position+(random_direction*spawn_radius)
	# 如果没有摄像机，直接返回默认位置
	#if player_camera == null:
		#return spawn_pos
	## 获取摄像机视口矩形
	#var viewport_size = get_tree().root.get_visible_rect().size
	#var camera_zoom = player_camera.zoom
	#var camera_position = player_camera.get_screen_center_position()
	#
	## 计算摄像机视口矩形，考虑缩放
	#var camera_rect = Rect2(
		#camera_position - (viewport_size / (2 * camera_zoom)),
		#viewport_size / camera_zoom
	#)
	## 扩大一点视口区域以确保生成位置完全在视野外
	#camera_rect = camera_rect.grow(50)
	## 尝试最多10次找到视野外的位置
	#var attempts = 0
	#while camera_rect.has_point(spawn_pos) and attempts < 10:
		#random_direction = Vector2(randf_range(-1, 1), randf_range(-1, 1)).normalized()
		#spawn_pos = player.global_position + random_direction * (min_distance_from_player + randf_range(0, spawn_radius - min_distance_from_player))
		#attempts += 1
	# 如果尝试多次仍在摄像机内，则强制将生成位置推到摄像机外
	#if camera_rect.has_point(spawn_pos):
		## 获取从玩家到该位置的方向，并延长距离
		#var dir_from_player = (spawn_pos - player.global_position).normalized()
		#spawn_pos = player.global_position + dir_from_player * (camera_rect.size.length() + 100)
	
	return spawn_pos

func spawn_wave():
	# 生成一波敌人
	var num_enemies = 10 * wave
	
	for i in range(num_enemies):
		spawn_enemy()
		await get_tree().create_timer(0.2).timeout
	
	# 如果是第5波或之后，生成Boss
	if wave >= 5 and wave % 5 == 0 and boss_scenes.size() > 0:
		spawn_boss()

func spawn_boss():
	if boss_scenes.size() == 0 or player == null:
		return
		
	var boss_scene = boss_scenes[randi() % boss_scenes.size()]
	var boss = boss_scene.instantiate()
	
	# 获取在摄像机视野外的Boss生成位置
	var spawn_pos = get_spawn_position_outside_camera()
	
	boss.global_position = spawn_pos
	add_child(boss)
	
	# 对Boss进行配置
	if boss.has_method("initialize"):
		boss.initialize(difficulty_modifier, wave)
	
	# 发出敌人生成信号
	enemy_spawned.emit(boss) 
